﻿using Easy.Common.Extension;
using Easy.Common.Helper;
using Easy.Common.Helper.JWT;
using Easy.Services.Entities;
using Easy.Services.Interface;
using Microsoft.AspNetCore.Mvc;

namespace Easy.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class SystemController(IUserService _userService, 
        IRoleService _roleService,
        IUserRoleRelationService _userRoleRelationService,
        IPermissionService _permissionService,
        IRolePermissionRelationService _rolePermissionRelationService,
        ICompanyService _companyService,
        IInterService _interService
        ) : ControllerBase
    {

        readonly IUserService userService = _userService;

        readonly IRoleService roleService = _roleService;

        readonly IUserRoleRelationService userRoleRelationService = _userRoleRelationService;

        readonly IPermissionService permissionService = _permissionService;

        readonly IRolePermissionRelationService rolePermissionRelationService = _rolePermissionRelationService;

        readonly ICompanyService companyService = _companyService;

        readonly IInterService interService = _interService;


        [HttpGet]
        public void Init()
        {
            long id = 1;
            DateTime createDate = DateTime.Now;
            string userName = "admin";
            InitAdmin(id, createDate, userName);
            InitRole(id, createDate, userName);
            InitUserRoleRelation(id, createDate, userName);
            InitPermission(id, createDate, userName);
            InitRolePermissionRelation(id, createDate, userName);
            InitCompany(id, createDate, userName);


        }

        private void InitAdmin(long id, DateTime createDate, string userName)
        {
            User user = new()
            {
                City = "henan",
                Company = "easy",
                CompanyId = 1,
                CorrectionTime = "2020-01-01",
                CreateIP = HttpContext.GetClientIP(),
                CreaterDate = createDate,
                CreaterId = 1,
                CreaterName = userName,
                DepartmentId = 1,
                DepartmentName = "easy",
                EditDate = createDate,
                EditId = id,
                EditName = userName,
                Email = "easy@easy.com",
                FormOfEmployment = 1,
                Gender = 1,
                IsDelete = false,
                IsFreeze = 0,
                LastLoginDate = createDate,
                LoginErrorCount = 0,
                LoginTotalCount = 0,
                Mobile = "18888666666"
            };
            string salt = CommonHelper.CreateVerifyCode(6);
            user.Password = CommonHelper.CalcMD5("123456" + salt);
            user.PasswordSalt = salt;
            user.TimeOfEntry = "2020-01-01";
            user.UserName = userName;
            user.WorkNumber = "000001";
            userService.Add(user);
        }


        private void InitRole(long id, DateTime createDate, string userName)
        {
            roleService.Add(new()
            {
                CreaterDate = createDate,
                CreaterId = id,
                CreaterName = userName,
                Description = userName,
                EditDate = createDate,
                EditId = id,
                EditName = userName,
                Name = "管理员",
                State = 1
            });
            roleService.Add(new()
            {
                CreaterDate = createDate,
                CreaterId = id,
                CreaterName = userName,
                Description = "系统组",
                EditDate = createDate,
                EditId = id,
                EditName = userName,
                Name = "系统组",
                State = 1
            });
            roleService.Add(new()
            {
                CreaterDate = createDate,
                CreaterId = id,
                CreaterName = userName,
                Description = "数据库",
                EditDate = createDate,
                EditId = id,
                EditName = userName,
                Name = "数据库",
                State = 1
            });

        }


        private void InitUserRoleRelation(long id, DateTime createDate, string userName)
        {
            userRoleRelationService.AddBatch([new()
            {
                CreaterDate = createDate,
                CreaterId = id,
                CreaterName = userName,
                EditDate = createDate,
                EditId = id,
                EditName = userName,
                RoleId = 1,
                UserId = 1
            }]);

        }


        private void InitPermission(long id, DateTime createDate, string userName)
        {
            //1
            permissionService.Add(new()
            {
                Code = "demo",
                CreaterDate = createDate,
                CreaterId = id,
                CreaterName = userName,
                Description = "demo",
                EditDate = createDate,
                EditId = id,
                EditName = userName,
                EnVisible = 0,
                Name = "demo",
                Pid = 0,
                Type = 1
            });
            //2
            permissionService.Add(new()
            {
                Code = "database",
                CreaterDate = createDate,
                CreaterId = id,
                CreaterName = userName,
                Description = "database",
                EditDate = createDate,
                EditId = id,
                EditName = userName,
                EnVisible = 1,
                Name = "数据库",
                Pid = 0,
                Type = 1
            });
            //3
            permissionService.Add(new()
            {
                Code = "department",
                CreaterDate = createDate,
                CreaterId = id,
                CreaterName = userName,
                Description = "department",
                EditDate = createDate,
                EditId = id,
                EditName = userName,
                EnVisible = 1,
                Name = "部门",
                Pid = 0,
                Type = 1
            });
            //4
            permissionService.Add(new()
            {
                Code = "employee",
                CreaterDate = createDate,
                CreaterId = id,
                CreaterName = userName,
                Description = "employee",
                EditDate = createDate,
                EditId = id,
                EditName = userName,
                EnVisible = 1,
                Name = "管理员",
                Pid = 0,
                Type = 1
            });
            //5
            permissionService.Add(new()
            {
                Code = "interface",
                CreaterDate = createDate,
                CreaterId = id,
                CreaterName = userName,
                Description = "interface",
                EditDate = createDate,
                EditId = id,
                EditName = userName,
                EnVisible = 1,
                Name = "接口",
                Pid = 0,
                Type = 1
            });
            //6
            permissionService.Add(new()
            {
                Code = "role",
                CreaterDate = createDate,
                CreaterId = id,
                CreaterName = userName,
                Description = "role",
                EditDate = createDate,
                EditId = id,
                EditName = userName,
                EnVisible = 1,
                Name = "角色",
                Pid = 0,
                Type = 1
            });
            //7
            permissionService.Add(new()
            {
                Code = "permission",
                CreaterDate = createDate,
                CreaterId = id,
                CreaterName = userName,
                Description = "permission",
                EditDate = createDate,
                EditId = id,
                EditName = userName,
                EnVisible = 1,
                Name = "权限",
                Pid = 0,
                Type = 1
            });
            //8
            permissionService.Add(new()
            {
                Code = "system",
                CreaterDate = createDate,
                CreaterId = id,
                CreaterName = userName,
                Description = "system",
                EditDate = createDate,
                EditId = id,
                EditName = userName,
                EnVisible = 1,
                Name = "系统",
                Pid = 0,
                Type = 1
            });


        }


        private void InitRolePermissionRelation(long id, DateTime createDate, string userName)
        {
            RolePermissionRelation p = new()
            {
                CreaterDate = createDate,
                CreaterId = id,
                CreaterName = userName,
                EditDate = createDate,
                EditId = id,
                EditName = userName,
                PermissionId = 1,
                RoleId = 1
            };
            List<RolePermissionRelation> list = [];
            for (int i = 1; i < 9; i++) {
                list.Add(new()
                {
                    CreaterDate = createDate,
                    CreaterId = id,
                    CreaterName = userName,
                    EditDate = createDate,
                    EditId = id,
                    EditName = userName,
                    PermissionId = i,
                    RoleId = 1
                });
            }
            rolePermissionRelationService.AddBatch(list);


        }


        private void InitCompany(long id, DateTime createDate, string userName)
        {
           
            companyService.Add(new()
            {
                Code = "000001",
                CreaterDate = createDate,
                CreaterId = id,
                CreaterName = userName,
                EditDate = createDate,
                EditId = id,
                EditName = userName,
                Introduce = "管理员",
                ManagerId = id,
                ManagerName = userName,
                Name = "管理员",
                Pid = 0
            });

            companyService.Add(new()
            {
                Code = "000002",
                CreaterDate = createDate,
                CreaterId = id,
                CreaterName = userName,
                EditDate = createDate,
                EditId = id,
                EditName = userName,
                Introduce = "系统组",
                ManagerId = id,
                ManagerName = userName,
                Name = "系统组",
                Pid = 0
            });

        }




    }
}
